/*
 * 文件名： BoundPhoneActivity.java
 * 描    述：绑定手机
 * 时    间：2015-3-25 下午5:47:21
 * 版    本：V1.0
 */
package com.miguo.activity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.miguo.activity.base.BaseActivity;
import com.miguo.framework.utils.Toast;
import com.miguo.ui.R;
import com.miguo.utils.StringUtils;
import com.miguo.view.CustomEditText;

public class BoundPhoneActivity extends BaseActivity implements OnClickListener {
    private CustomEditText mEtPhone, mEtVerificationCode;
    private Button mBtnVerificationCode;
    private String mPhone, mVerificationCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bound_phone);
        initView();
    }

    @Override
    protected int getStatusBarColorResId() {
        return R.color.red_ff3366;
    }

    private void initView() {
        // 手机号码
        mEtPhone = (CustomEditText) findViewById(R.id.bound_etPhone);
        // 验证码
        mEtVerificationCode = (CustomEditText) findViewById(R.id.bound_verificationCode);
        // 获取验证码
        mBtnVerificationCode = (Button) findViewById(R.id.bound_btnGetCode);
        mBtnVerificationCode.setOnClickListener(this);
        // 确定
        findViewById(R.id.bound_btnCommit).setOnClickListener(this);
    }

    // 验证输入内容
    private void verification() {
        mPhone = mEtPhone.getString();
        if (!StringUtils.isPhoneNumber(mPhone)) {
            Toast.show(mActivity, "手机号码格式错误");
            return;
        }
        mVerificationCode = mEtVerificationCode.getString();
        if (TextUtils.isEmpty(mVerificationCode)) {
            Toast.show(mActivity, "请输入验证码");
            return;
        }
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.bound_btnGetCode:
                mPhone = mEtPhone.getString();
                if (!StringUtils.isPhoneNumber(mPhone)) {
                    Toast.show(mActivity, "手机号码格式错误");
                } else {
                    new myCount(60000, 1000).start();
                }
                break;
            case R.id.bound_btnCommit:
                verification();
                break;

        }
    }

    // 计时器
    class myCount extends CountDownTimer {

        public myCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
            // TODO Auto-generated constructor stub
        }

        @Override
        public void onFinish() {
            // TODO Auto-generated method stub
            mBtnVerificationCode.setEnabled(true);
            mBtnVerificationCode.setText("获取验证码");
        }

        @Override
        public void onTick(long arg0) {
            // TODO Auto-generated method stub
            mBtnVerificationCode.setEnabled(false);
            mBtnVerificationCode.setText(arg0 / 1000 + "秒后重新获取");
        }
    }

}
